package com.fanco.learn_datastruct_leetcode.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author qiaowang 2021/7/29
 */
public class Test {

    public static void main(String[] args) {

        System.out.println(getRandomValue());

        Solution solution = new Solution();
        solution.convert("PAYPALISHIRING", 3);
    }

    /**
     * * 将一个给定字符串 s 根据给定的行数 numRows ，以从上往下、从左到右进行 Z 字形排列。
     * * <p>
     * * 比如输入字符串为 "PAYPALISHIRING" 行数为 3 时，排列如下：
     * * <p>
     * * P   A   H   N
     * * A P L S I I G
     * * Y   I   R
     */
    private static class Solution {
        public String convert(String s, int numRows) {
            if (numRows == 1) {
                return s;
            }

            int maxRows = s.length() > numRows ? numRows : s.length();

            List<StringBuilder> res = new ArrayList<>();
            for (int i = 0; i < maxRows; i++) {
                res.add(new StringBuilder());
            }

            boolean flag = false;
            int curRow = 0;
            for (int i = 0; i < s.length(); i++) {
                res.get(curRow).append(s.charAt(i));
                if (curRow == 0 || curRow == maxRows - 1) {
                    flag = !flag;
                }
                curRow += flag? 1 : -1;
            }

            for (int i = 0; i < res.size(); i++) {
                System.out.println(res.get(i).toString());
            }


            return "";
        }
    }

    private static String getRandomValue() {

        int randomQuestionNumber = new Random().nextInt(24);
        return "今天刷第" + randomQuestionNumber + "题";
    }

}
